package com.blockchain.securewallet.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.blockchain.securewallet.R;
import com.blockchain.securewallet.WalletApp;
import com.blockchain.securewallet.adapter.AddressAdapter;
import com.blockchain.securewallet.base.BaseActivity;
import com.blockchain.securewallet.model.db.BlockAddress;
import com.blockchain.securewallet.model.db.CoinWallet;
import com.blockchain.securewallet.model.db.dao.BlockAddressDao;
import com.blockchain.securewallet.utils.Logger;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddressBookActivity extends BaseActivity implements AddressAdapter.IOnClickListener {

    private static final String TAG = "Wallet.AddressBook";

    private int type = -1;
    private int pos = -1;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.rv_address)
    RecyclerView rvAddresses;

    @BindView(R.id.btn_add_address)
    Button btnAddAddress;

    private AddressAdapter adapter = null;
    private List<BlockAddress> addresses;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_book);
        ButterKnife.bind(this);
        tvTitle.setText(R.string.slide_address_book);

        Intent intent = getIntent();
        if (intent != null) {
            type = intent.getIntExtra(SendCoinActivity.KEY_TYPE_COIN, -1);
            pos = intent.getIntExtra(SendCoinActivity.KEY_POS, -1);

            //Logger.ds("> type: " + type + "\tpos: " + pos);
            if (type != -1 && pos != -1)
                btnAddAddress.setVisibility(View.GONE);
        }
//        addresses = new ArrayList<>();
//        BlockAddress address = new BlockAddress();
//        address.setAddress("my first editAddress");
//        address.setNote("测试地址");
//        address.setType(BlockAddress.TYPE_BIT_CON);
//        addresses.add(address);
//        address = new BlockAddress();
//        address.setAddress("my ether editAddress");
//        address.setNote("");
//        address.setType(BlockAddress.TYPE_ETH_COIN);
//        addresses.add(address);
        adapter = new AddressAdapter(this, addresses);
        adapter.setListener(this);
        rvAddresses.setLayoutManager(new LinearLayoutManager(this));
        rvAddresses.setAdapter(adapter);
    }

    @OnClick(R.id.btn_back)
    void back() {
        finish();
    }

    @OnClick(R.id.btn_add_address)
    void addAddress() {
        Intent intent = new Intent();
        intent.setClass(this, AddressAddActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //Logger.ds(">> type: " + type + "\tpos: " + pos);

        if (type == -1 || pos == -1) {
            addresses = WalletApp.getInstance().getDaoSession().loadAll(BlockAddress.class);
            //Logger.ds("全部的addresses容量: " + addresses.size());
            
           /* //这个是用来修复前人关于从发送界面选择以太坊却不显示的bug的
            BlockAddressDao addressDao = WalletApp.getInstance().getDaoSession().getBlockAddressDao();
            for (int i = 0; i < addresses.size(); i++) {
                BlockAddress address = addresses.get(i);
                Logger.d("\n" + address.toString() + "\n");

                if (address.getType() == -1) {
                    BlockAddress entity = new BlockAddress();
                    entity.setId(address.getId());
                    entity.setType(1);
                    entity.setNote(address.getNote());
                    entity.setAddress(address.getAddress());
                    Logger.ds(address.getId() + ":\tupdate BlockAddress set type = 1  where type == -1");
                    addressDao.update(entity);
                }
            }*/

        } else {
            addresses = WalletApp.getInstance().getDaoSession().getBlockAddressDao()
                    .queryBuilder()
                    .where(BlockAddressDao.Properties.Type.eq(type == CoinWallet.TYPE_BIT_COIN ? BlockAddress.TYPE_BIT_CON : BlockAddress.TYPE_ETH_COIN))
                    .list();

            Logger.ds("筛选后的addresses容量: " + addresses.size());
        }
        adapter.setAddresses(addresses);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(int type, String address, String note) {
        if (type == -1 || pos == -1) {
            Logger.ds("前往地址详情 type: " + type + "\tAddressInfoActivity界面 pos: " + pos);

            Intent intent = new Intent("com.blockchain.securewallet.action.ADDRESSINFO");
            intent.addCategory("android.intent.category.DEFAULT");
            intent.putExtra(AddressInfoActivity.KEY_ADDRESS_TYPE, type);
            intent.putExtra(AddressInfoActivity.KEY_ADDRESS_CONTENT, address);
            intent.putExtra(AddressInfoActivity.KEY_ADDRESS_NOTE, note);
            startActivity(intent);
        } else {
            Logger.ds("前往发送货币 type: " + type + "\tSendCoinActivity界面 pos: " + pos);
            //从发送页面过来选择的会回到发送页面(带值)

            Intent intent = new Intent();
            intent.putExtra(SendCoinActivity.KEY_ADDRESS, address);
            intent.putExtra(SendCoinActivity.KEY_POS, pos);
            setResult(0, intent);
            finish();
        }
    }
    
}
